package com.keiou.drink.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.keiou.drink.AppContext;
import com.keiou.drink.api.converter.CustomGsonConverterFactory;
import com.keiou.drink.api.converter.DoubleDefault0Adapter;
import com.keiou.drink.api.converter.IntegerDefault0Adapter;
import com.keiou.drink.api.converter.LongDefault0Adapter;
import com.keiou.drink.util.TLog;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * 作者 ：Bob on 2017/6/1.
 * 邮箱 ：motherlovefather@126.com
 */

public class ServiceFactory {

    private static final String TAG = "network_log";
    private static OkHttpClient okHttpClient;
    private static HttpLoggingInterceptor loggingInterceptor;
    private static Interceptor interceptor;
    private static Gson gson;

    public static <T> T createService(String baseUrl, Class<T> serviceClazz) {
        Retrofit retrofit = new Retrofit.Builder()
                .client(getOkHttpClient())
//                .addConverterFactory(GsonConverterFactory.create(buildGson()))
                .addConverterFactory(CustomGsonConverterFactory.create(buildGson()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();
        return retrofit.create(serviceClazz);
    }

    public static OkHttpClient getOkHttpClient() {
        if (null == okHttpClient) {
            okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(getHttpLoggingInterceptor())
                    // .addInterceptor(getInterceptor())
                    .connectTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .build();
        }
        return okHttpClient;
    }

    public static HttpLoggingInterceptor getHttpLoggingInterceptor() {
        if (null == loggingInterceptor) {
            loggingInterceptor = new HttpLoggingInterceptor(message -> TLog.i(TAG, message));
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        }
        return loggingInterceptor;
    }

    /**
     * 设置Header
     *
     * @return
     */
    public static Interceptor getInterceptor() {
        if (null == interceptor) {
            Map<String, String> heards = AppContext.getInstance().getRequestHeader();
            interceptor = chain -> {
                Request original = chain.request();
                if (null != heards && heards.size() > 0) {
                    Request.Builder builder = original.newBuilder();
                    Iterator<String> it = heards.keySet().iterator();
                    while (it.hasNext()) {
                        String key = it.next();
                        String value = heards.get(key);
                        builder.addHeader(key, value);
                    }
                    builder.method(original.method(), original.body());
                    return chain.proceed(builder.build());
                }
                return chain.proceed(original);
            };
        }

        return interceptor;
    }

    public static Gson buildGson() {
        if (null == gson) {
            gson = new GsonBuilder()
                    .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                    .registerTypeAdapter(long.class, new LongDefault0Adapter())
                    .create();
        }
        return gson;
    }

}
